package com.shanghai.shanghaielectric;

import android.content.Context;
import android.os.Environment;

import com.facebook.stetho.Stetho;

import org.litepal.LitePal;
import org.litepal.LitePalApplication;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 45000460 on 2017/11/20.
 */

public class MyApplication extends LitePalApplication{
    private static Context mComtext;
    @Override
    public void onCreate() {
        super.onCreate();
        mComtext=this.getApplicationContext();
        Stetho.initializeWithDefaults(this);
        createDatabase();
        LitePal.initialize(this);
    }

    public static Context getContext() {
        return mComtext;
    }
    private void createDatabase() {
         int BUFFER_SIZE = 200000;
         String DB_NAME = "configinfo_apf_2.db"; //保存的数据库文件名
         String PACKAGE_NAME = "com.shanghai.shanghaielectric";
         String DB_PATH = "/data"
                + Environment.getDataDirectory().getAbsolutePath() + "/"
                + PACKAGE_NAME;  //在手机里存放数据库的位置
         String dbPath = DB_PATH + "/databases/";
         String dbfile = dbPath + DB_NAME;
        try {
            if (!(new File(dbfile).exists())) {//判断数据库文件是否存在，若不存在则执行导入
                ///
                File filepath = new File(dbPath);
                if (!filepath.exists()) {
                    filepath.mkdirs();
                }
                ///
                if (filepath.exists()) {
                    InputStream is = mComtext.getResources().openRawResource(
                            R.raw.configinfo_apf_2); //欲导入的数据库
                    FileOutputStream fos = new FileOutputStream(dbfile);
                    byte[] buffer = new byte[BUFFER_SIZE];
                    int count = 0;
                    while ((count = is.read(buffer)) > 0) {
                        fos.write(buffer, 0, count);
                    }
                    fos.close();
                    is.close();
                }
            }
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }
    }


}
